// pages/flag_group/flag_group.js
var common = require("../../common/common.js").common;

var colorParse = require("../../common/color.js");

var {$author: $author} = require("../../components/base/authorization.js");

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        alert: 0,
        search_con: "",
        chooseClass: 1,
        date_check: false,
        dateMonth: "历史",
        plat_time: [ {
            text: "今日",
            date: "TdateFormate",
            type: 1
        }, {
            text: "昨日",
            date: "YdateFormate",
            type: 2
        }, {
            text: "本周",
            date: "WeekFormate",
            type: 3
        }, {
            text: "本月",
            date: "MonthFormate",
            type: 4
        } ],
        page: 1
    },
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        var that = this;
        that.setData({
            page: 1,
            group: [],
            clerk: wx.getStorageSync("clerk")
        });
        that.getLeaderUnderData();
        wx.stopPullDownRefresh();
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        let leader_id = options.leader_id;
        this.setData({
            leader_id: leader_id,
            clerk: wx.getStorageSync("clerk"),
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getBase();
        this.getLeaderUnderData();
        this.getData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getLeaderUnderData();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        var that = this;
        var leader_id = that.data.leader_id;
        var path = "/pages/group_info/group_info?my_leaderID=" + leader_id;
        return {
            title: "旗下" + that.data.leaderName,
            path: path
        };
    },
    //搜索关键词
    getValue: function(e) {
        var that = this;
        var search_con = e.detail.value;
        that.setData({
            search_con: search_con
        });
    },
    search: function(e) {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        var search = that.data.search_con;
        if (search == "") {
            wx.showModal({
                title: "提示",
                content: "请输入搜索关键词",
                showCancel: false,
                success: function() {
                    return false;
                }
            });
        }
        common.request({
            endpoint: "Leader/undermember/underleader",
            method: "post",
            data: {
                openid: openid,
                search: search
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    group: res.extra
                });
            }
        });
    },
    getinfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Leader/undermember/underleader",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    group: res.extra
                });
            }
        });
    },
    //跳转共用生成海报页面
    makeposter: function() {
        wx.navigateTo({
            url: "/pages/share/share"
        });
    },
    callphone: function(e) {
        var phone = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: phone
        });
    },
    bindDateChange(e) {
        this.setData({
            dateMonth: e.detail.value,
            date_check: false,
            chooseClass: "5"
        });
        this.getLeaderUnderData();
    },
    bindDateMonthChange(e) {
        this.setData({
            dateMonth: e.detail.value,
            date_check: false,
            chooseClass: "6"
        });
        this.getLeaderUnderData();
    },
    dateCheckChange() {
        this.setData({
            date_check: !this.data.date_check
        });
    },
    alert: function() {
        var alert = this.data.alert;
        if (alert == 0) {
            alert = 1;
        } else {
            alert = 0;
        }
        this.setData({
            alert: alert
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                var rgba = colorParse.hexToRgb(res.extra.color_main, .8);
                that.setData({
                    color_main: res.extra.color_main,
                    color_main2: rgba,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on,
                    title: "旗下" + res.extra.leader_title ? res.extra.leader_title : "团长",
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                wx.setNavigationBarTitle({
                    title: "旗下" + res.extra.leader_title ? res.extra.leader_title : "团长"
                });
            }
        });
    },
    queryData: function(e) {
        let that = this;
        that.setData({
            chooseClass: e.currentTarget.dataset.type,
            page: 1,
            group: []
        });
        that.getLeaderUnderData();
    },
    getLeaderUnderData: function() {
        let that = this;
        var openid = wx.getStorageSync("user").openid;
        var params = {
            openid: openid,
            type: that.data.chooseClass,
            page: that.data.page
        };
        if (that.data.chooseClass == "5" || that.data.chooseClass == "6") {
            params["time"] = that.data.dateMonth;
        }
        common.request({
            endpoint: "My/leader_under/data",
            method: "post",
            data: params,
            header: {
                "content-type": "application/json"
            },
            success: res => {
                let group = res.extra.each_data;
                that.setData({
                    plat_data: res.extra
                });
                if (group.length > 0) {
                    group = that.data.page == 1 ? group : that.data.group.concat(group);
                    that.setData({
                        group: group,
                        page: that.data.page + 1
                    });
                }
            }
        });
    },
    getData: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
            }
        });
    },
    goToOtherFunction(e) {
        common.getUserInfoNew({
            url: e.currentTarget.dataset.url
        });
    }
});